#!/bin/bash
#################
### aur2go ######
version="0.3" ###
#################
### OS Hazard ###
#################

apacman="/usr/bin/apacman";
exit_status() 
{ 
  echo "::: Failed. Error at $@"; 
  exit 1; 
}
check_root() 
{ 
  if [ "$UID" = "0" ] || [ "$USER" = "root" ]; 
  then sleep 0;
  else echo "no"; 
  fi;
}

echo "::: aur2go installer";
check_root
echo ":: Syncing time";
date -s "$(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g')" || exit_status "syncing date";

echo ":: Updating dependencies";
rm /var/lib/pacman/db.lck 2>/dev/null;
pacman -Sy || exit_status "syncing databases";
packwork=$(pacman -Ss coreutils 2>/dev/null);
[ -z "$packwork" ] && rm /var/lib/pacman/sync/* && pacman -Sy;
packwork=$(pacman -Ss coreutils 2>/dev/null);
[ -z "$packwork" ] && exit_status "pacman database broken";
sed -i 's/^SyncFirst/#_aur2go_SyncFirst/g' /etc/pacman.conf || exit_status "ignoring syncfirst";
pacman --noconfirm -S ca-certificates git grep jshon pcre || exit_status "installing dependencies";

echo ":: Installing packer";
mkdir -p /opt/aur/packer || exit_status "creating directory";
cd /opt/aur/packer;
wget http://tinyurl.com/get-packer -O PKGBUILD || exit_status "downloading packer build";
makepkg --asroot --noconfirm -i || exit_status "installing packer";
sed -i 's/^#_aur2go_SyncFirst/SyncFirst/g' /etc/pacman.conf || exit_status "fixing syncfirst";

echo ":: Patching packer";
packer=$(which packer 2>/dev/null);
cp "$packer" "$apacman";
line="1";
sed -i "${line}a#################" $apacman; line=$((line+1));
sed -i "${line}a### apacman #####" $apacman; line=$((line+1));
sed -i "${line}aversion=""\"0.3""\" ###" $apacman; line=$((line+1));
sed -i "${line}a### OS Hazard ###" $apacman; line=$((line+1));
sed -i "${line}a#################" $apacman; line=$((line+1));
sed -i "${line}a#" $apacman; line=$((line+1));
sed -i "${line}a# Patched version of packer" $apacman; line=$((line+1));
sed -i "${line}a# Features:" $apacman; line=$((line+1));
sed -i "${line}a# * ARM support" $apacman; line=$((line+1));
sed -i "${line}a# * Saves AUR packages" $apacman; line=$((line+1));
sed -i "${line}a#" $apacman; 
line=$(grep -n "pacmanconf=" "$apacman" | awk -F ":" '{print $1}');
sed -i "${line}asaveDir='/var/cache/packer/pkg'" $apacman; 
line=$(grep -n "The build failed" "$apacman" | awk -F ":" '{print $1}');
sed -i "${line}a%%%" $apacman; line=$((line+1));
sed -i "${line}a%%%  if [ ! -d \$saveDir ]; then" $apacman; line=$((line+1));
sed -i "${line}a%%%    runasroot mkdir -p \$saveDir" $apacman; line=$((line+1));
sed -i "${line}a%%%  fi" $apacman; line=$((line+1));
sed -i "${line}a%%%  runasroot cp \$pkgname-*\$PKGEXT \$saveDir/" $apacman; line=$((line+1));
sed -i "${line}a%%%  echo ""\"Saved: \$(ls \$pkgname-*\$PKGEXT)""\";" $apacman; line=$((line+1));
sed -i "${line}a%%%" $apacman;
sed -i 's/^%%%//g' $apacman;
sed -i 's/makepkg $MAKEPKGOPTS/makepkg -A $MAKEPKGOPTS/g' $apacman;

echo ":: Testing AUR";
[ ! -z "$(apacman -Ss chronos-firmware | awk NR==1)" ] && echo "::: Success" || exit_status "testing packer";

echo "USAGE: apacman [option] [package]";
### END ###
